package com.explorati.LeetCode014.longestCommonPrefix;

/**
 * @ Author : Weijian_Wang
 * @ Date : Created in 18:21 2020/9/20 0020
 * @ Description ：
 */
public class Solution1 {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0){
            return "";
        }
        String ans = strs[0];
        for(int i =1;i<strs.length;i++) {
            int j=0;
            for(;j<ans.length() && j < strs[i].length();j++) {
                if(ans.charAt(j) != strs[i].charAt(j)){
                    break;
                }
            }
            //每次必截取，防止出现 ans为"aaa"，strs[i]为"a"的情况
            ans = ans.substring(0, j);
            if(ans.equals("")){
                return ans;
            }
        }
        return ans;

    }
}
